package org.jeecg.modules.demo.Productdetail.service.impl;

import org.jeecg.modules.demo.Productdetail.entity.Productdetail;
import org.jeecg.modules.demo.Productdetail.entity.Pdetail;
import org.jeecg.modules.demo.Productdetail.mapper.PdetailMapper;
import org.jeecg.modules.demo.Productdetail.mapper.ProductdetailMapper;
import org.jeecg.modules.demo.Productdetail.service.IProductdetailService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.util.List;
import java.util.Collection;

/**
 * @Description: 产品明细
 * @Author: jeecg-boot
 * @Date:   2021-02-15
 * @Version: V1.0
 */
@Service
public class ProductdetailServiceImpl extends ServiceImpl<ProductdetailMapper, Productdetail> implements IProductdetailService {

	@Autowired
	private ProductdetailMapper productdetailMapper;
	@Autowired
	private PdetailMapper pdetailMapper;
	
	@Override
	@Transactional
	public void saveMain(Productdetail productdetail, List<Pdetail> pdetailList) {
		productdetailMapper.insert(productdetail);
		if(pdetailList!=null && pdetailList.size()>0) {
			for(Pdetail entity:pdetailList) {
				//外键设置
				entity.setPid(productdetail.getId());
				pdetailMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional
	public void updateMain(Productdetail productdetail,List<Pdetail> pdetailList) {
		productdetailMapper.updateById(productdetail);
		
		//1.先删除子表数据
		pdetailMapper.deleteByMainId(productdetail.getId());
		
		//2.子表数据重新插入
		if(pdetailList!=null && pdetailList.size()>0) {
			for(Pdetail entity:pdetailList) {
				//外键设置
				entity.setPid(productdetail.getId());
				pdetailMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional
	public void delMain(String id) {
		pdetailMapper.deleteByMainId(id);
		productdetailMapper.deleteById(id);
	}

	@Override
	@Transactional
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			pdetailMapper.deleteByMainId(id.toString());
			productdetailMapper.deleteById(id);
		}
	}
	
}
